package com.goldcn.classify.service;

import com.goldcn.classify.dao.ClassifyAttrDefMapper;
import com.goldcn.classify.dao.ClassifyAttrValMapper;
import com.goldcn.classify.model.ClassifyAttrVal;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/25.
 */
@Service("classifyAttrValService")
@Transactional
public class ClassifyAttrValService {
    @Autowired
    private ClassifyAttrValMapper classifyAttrValMapper;

    @Autowired
    private ClassifyAttrDefMapper classifyAttrDefMapper;

    public int deleteByPrimaryKey(String cavID) {
        return classifyAttrValMapper.deleteByPrimaryKey(cavID);
    }

    public int insertSelective(ClassifyAttrVal record) {
        record.setCavID(UuidUtils.uuid());
        int i=classifyAttrValMapper.insertSelective(record);
        if(i!=1){
            throw new ServiceException("插入属性选项值失败", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return i;
    }

    public ClassifyAttrVal selectByPrimaryKey(String cavID) {
        return classifyAttrValMapper.selectByPrimaryKey(cavID);
    }

    public int updateByPrimaryKeySelective(ClassifyAttrVal record) {
        int i=classifyAttrValMapper.updateByPrimaryKeySelective(record);
        if(i!=1){
            throw new ServiceException("修改属性选项值失败", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return i;
    }

    public List<ClassifyAttrVal> selectByAttrDef(String cfadid){
        return classifyAttrValMapper.selectByAtrDef(cfadid);
    }

    @Transactional
    public boolean  deleteByAttrDef(String cfadID){
        List<ClassifyAttrVal> list=new ArrayList<ClassifyAttrVal>();
        list=classifyAttrValMapper.selectByAtrDef(cfadID);
        if(!list.isEmpty()){
            for(ClassifyAttrVal classifyAttrVal:list){
                classifyAttrValMapper.deleteByPrimaryKey(classifyAttrVal.getCavID());
            }
        }
        classifyAttrDefMapper.deleteByPrimaryKey(cfadID);
        return true;
    }


}
